﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AspNetTraining.BLL
{
  public  class PermissionManager
    {
      /// <summary>
      /// Get all permission where active =true
      /// </summary>
      /// <returns></returns>
      public List<Permission> GetAllPermission()
      {
          using (var context = new DotNetTrainingEntities())
          {
              return context.Permissions.ToList<Permission>() ;
          };
      }
      /// <summary>
      /// Return one Permisstion by Id
      /// </summary>
      /// <param name="Id"></param>
      /// <returns></returns>
      public Permission GetPermissionById(System.Guid ID)
      {
          using (var context = new DotNetTrainingEntities()) {
              return context.Permissions.Where(p => p.PermissionID == ID).FirstOrDefault();
          };
      }
      /// <summary>
      /// Edit permission
      /// </summary>
      /// <param name="per"></param>
      /// <returns></returns>
      public bool UpdatePermission(Permission per)
      {
          using (var context = new DotNetTrainingEntities())
          {
              context.Entry(per).State = EntityState.Modified;
              context.SaveChanges();
          }
          return true;
      }
      /// <summary>
      /// Insert permission
      /// </summary>
      /// <param name="per"></param>
      /// <returns></returns>
      public bool InsertPermission(Permission per)
      {
          using (var context = new DotNetTrainingEntities())
          {
              context.Permissions.Add(per);
              try
              {
                  context.SaveChanges();
                  return true;
              }
              catch { return false; }

          }
      }
      /// <summary>
      /// Delete Permission
      /// </summary>
      /// <param name="Id"></param>
      /// <returns></returns>
      public bool DeletePermission(System.Guid Id)
      {
          using (var context = new DotNetTrainingEntities())
          {
              try
              {
                  Permission user = context.Permissions.Where(scc => scc.PermissionID == Id).FirstOrDefault();
                  context.Entry(user).State = EntityState.Deleted;
                  context.SaveChanges();
                  return true;
              }
              catch { return false; }
          }
      }

    }
}
